package com.ruoyi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.CategoryOne;
import com.ruoyi.domain.CategoryThree;
import com.ruoyi.domain.CategoryTwo;
import com.ruoyi.mapper.CategoryOneMapper;
import com.ruoyi.mapper.CategoryThreeMapper;
import com.ruoyi.mapper.CategoryTwoMapper;
import com.ruoyi.service.ICategoryOneService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 一级分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-14
 */
@Service
public class CategoryOneServiceImpl implements ICategoryOneService {
    @Autowired
    private CategoryOneMapper categoryOneMapper;

    @Autowired
    private CategoryTwoMapper categoryTwoMapper;

    @Autowired
    private CategoryThreeMapper categoryThreeMapper;

    /**
     * 查询一级分类
     *
     * @param coneId 一级分类主键
     * @return 一级分类
     */
    @Override
    public CategoryOne selectCategoryOneByConeId(Long coneId) {
        return categoryOneMapper.selectCategoryOneByConeId(coneId);
    }

    /**
     * 查询一级分类列表
     *
     * @param categoryOne 一级分类
     * @return 一级分类
     */
    @Override
    public List<CategoryOne> selectCategoryOneList(CategoryOne categoryOne) {
        return categoryOneMapper.selectCategoryOneList(categoryOne);
    }

    /**
     * 新增一级分类
     *
     * @param categoryOne 一级分类
     * @return 结果
     */
    @Override
    public int insertCategoryOne(CategoryOne categoryOne) {
        categoryOne.setCreateTime(DateUtils.getNowDate());
        return categoryOneMapper.insertCategoryOne(categoryOne);
    }

    /**
     * 修改一级分类
     *
     * @param categoryOne 一级分类
     * @return 结果
     */
    @Override
    public int updateCategoryOne(CategoryOne categoryOne) {
        categoryOne.setUpdateTime(DateUtils.getNowDate());
        return categoryOneMapper.updateCategoryOne(categoryOne);
    }

    /**
     * 批量删除一级分类
     *
     * @param coneIds 需要删除的一级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryOneByConeIds(Long[] coneIds) {
        return categoryOneMapper.deleteCategoryOneByConeIds(coneIds);
    }

    /**
     * 删除一级分类信息
     *
     * @param coneId 一级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryOneByConeId(Long coneId) {
        return categoryOneMapper.deleteCategoryOneByConeId(coneId);
    }

    @Override
    public List<CategoryOne> selectCategoryOneListAll() {
        return categoryOneMapper.selectCategoryOneListAll();
    }

    @Override
    public  List<Map<String, Object>> selectListCascader() {
        List<CategoryOne> oneList = categoryOneMapper.selectCategoryOneListAll();
        List<CategoryTwo> twoList = categoryTwoMapper.selectCategoryTwoListAll();
        List<CategoryThree> thrList = categoryThreeMapper.selectCategoryThreeListAll();

        // 循环一级
        List<Map<String, Object>> data = oneList.stream().map(one -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", one.getConeId());
            map.put("label", one.getConeName());
            // 根据 一级 id 为外键  查询 二级的集合
            List<Map<String, Object>> twoDataList = twoList.stream()
                    .filter(two -> one.getConeId().equals(two.getConeId()))
                    .map(two -> {
                        Map<String, Object> twoMap = new HashMap<>();
                        twoMap.put("value", two.getCtwoId());
                        twoMap.put("label", two.getCtwoName());
                        // 根据 二级id 为外键  查询 三级的集合
                        List<Map<String, Object>> thrDataList = thrList.stream()
                                .filter(thr -> two.getCtwoId().equals(thr.getCtwoId()))
                                .map(thr -> {
                                    Map<String, Object> thrMap = new HashMap<>();
                                    thrMap.put("value", thr.getCthrId());
                                    thrMap.put("label", thr.getCthrName());
                                    return thrMap;
                                }).collect(Collectors.toList());
                        twoMap.put("children", thrDataList);
                        return twoMap;
                    }).collect(Collectors.toList());
            map.put("children", twoDataList);
            return map;
        }).collect(Collectors.toList());




        return data;
    }
}
